//
//  YZComplicationsViewController.m
//  kuaiyi
//
//  Created by WGP on 16/5/4.
//
//

#import "YZComplicationsViewController.h"
#import "YZComplicationModel.h"
#import "YZDiseaseTypeViewModel.h"
#import "YZDiseaseTypeModel.h"
#import "YZComplicationCell.h"

@interface YZComplicationsViewController ()

@property(nonatomic,strong)NSMutableArray *selectedModels;
@property(nonatomic,strong)UILabel *label;

@end
 static NSString *ReuseIdentifier = @"tableViewCell";
@implementation YZComplicationsViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"并发症";
    [self.tableView registerClass:[YZComplicationCell class] forCellReuseIdentifier:ReuseIdentifier];
    
    [self setupLabel];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureBtnDidClick)];
    
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"jiantou"] style:UIBarButtonItemStylePlain target:self action:@selector(arrowItemDidClick)];

    if (self.models.count == 0) {
        [YZDiseaseTypeViewModel loadComplicationWith:self.ci1_id ci2_id:[self.diseastType.ci2_id integerValue] finished:^(NSArray *array, NSError *error) {
            if (error != nil) {
                NSLog(@"加载网络出错!");
            }
            self.models = array;
            
            if (self.models.count == 0) {
                self.label.hidden = NO;
                self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                self.tableView.bounces = NO;
            }
            
            [self.tableView reloadData];
        }];
    }
}

-(void)arrowItemDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setupLabel
{
    
    UILabel *label = [[UILabel alloc] init];
    label.hidden = YES;
    self.label = label;
    [self.tableView addSubview:label];
    label.text = @"无并发症信息";
    [label sizeToFit];
    label.center = CGPointMake(self.view.center.x, [UIScreen mainScreen].bounds.size.height*0.4);
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YZComplicationCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentifier forIndexPath:indexPath];
    cell.model = self.models[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     YZComplicationModel *model = self.models[indexPath.row];
    
    YZComplicationCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        model.isSelected = NO;
    }
    else if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        model.isSelected = YES;

    }
    
}

-(void)sureBtnDidClick
{
    for (YZComplicationModel *model in self.models) {
        if (model.isSelected) {
            [self.selectedModels addObject:model];
        }
    }
    
    if (self.selectedModels.count != 0) {
        NSMutableString *str = [NSMutableString string];
        [str appendString:@"["];
        if (self.selectedModels.count == 1) {
            
            [str appendString:[self.selectedModels.lastObject complication_name]];
            [str appendString:@"]"];
        }
        else
        {
            for (YZComplicationModel *model in self.selectedModels) {
                [str appendString:[NSString stringWithFormat:@"%@,",model.complication_name]];
            }
            [str replaceCharactersInRange:NSMakeRange(str.length-1, 1) withString:@""];
            [str appendString:@"]"];
        }
        NSString *countStr = [NSString stringWithFormat:@"(共%zd条)",self.selectedModels.count];
        [str appendString:countStr];
        
        if(str.length > 20)
        {
            [str replaceCharactersInRange:NSMakeRange(10, str.length-20) withString:@"..."];
        }
        
        NSRange range = [str rangeOfString:countStr];
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
        
        if (self.complicationModelBlock) {
            self.complicationModelBlock(attr,self.models);
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(NSMutableArray *)selectedModels
{
    if (_selectedModels == nil) {
        
        _selectedModels = [NSMutableArray array];
    }
    return _selectedModels;
}

@end










